#include "Juzmain.h"

#include <cstring>
#include <iostream>
#include <string>

#include "mainPlugin.h"
#include "public/event/OnCommandJuzEvent.h"
#include "public/event/OnDisableJuzEvent.h"
#include "public/event/OnEnableJuzEvent.h"
#include "PluginManager.h"

using namespace std;
static const char* g_pPluginPath = "./plugin/";

Juzmain::Juzmain()
{
    mPluginManager = PluginManager::getInstance();
	mMainPl = mainPlugin::getInstance();
}
Juzmain::~Juzmain()
{

}

void Juzmain::Init()
{
    cout<<"插件系统开始初始化!"<<endl;

    JuzLog::Init(mPluginManager);
	mPluginManager->loadFilePlugin(g_pPluginPath);
	mPluginManager->registeredPlugin(mMainPl);
	mPluginManager->Init();

	//发送 onEnable 事件
	OnEnableJuzEvent *eventOnEnable = new OnEnableJuzEvent();
	mPluginManager->sendEvent(eventOnEnable);
}
int main ()
{

	Juzmain *mainNow = new Juzmain();
    mainNow->Init();

    char *strCommand = new char[1024];

    OnCommandJuzEvent *onCommandEv = new OnCommandJuzEvent;
    string line;
    while(1)
    {
    	getline(cin,line);
    	strcpy(strCommand,line.c_str());
    	if(strlen(strCommand) > 1)
    	{
        	cout<<"执行命令:"<<strCommand<<endl;
        	if(line == "quit")
        	{
        		break;
        	}
        	else
        	{
        		onCommandEv->setCommand(strCommand);
        		mainNow->mPluginManager->sendEvent(onCommandEv);
        	}
    	}
    	else
    	{
    		cout<<"请输入正确的指令!"<<endl;
    	}
    }

    //通知模块退出
    OnDisableJuzEvent ev;
    mainNow->mPluginManager->sendEvent(&ev);

    cout<<"所有模块 均已关闭!"<<endl;
    delete mainNow;
    delete onCommandEv;
    delete[] strCommand;

    return 0;
}
